﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace ALDIOrganizer
{
    public static class DateTimeExtensionMethods
    {
        public static int WeekOfYear(this DateTime pMonday)
        {
            return new GregorianCalendar().GetWeekOfYear(pMonday,
                CalendarWeekRule.FirstFourDayWeek,
                DayOfWeek.Monday);
        }

        public static DateTime MondayOfThatWeek(this DateTime pDay)
        {
            var _date = pDay.Date;
            int _daysToSubstract; // how many days we need to substract? If datetime is date - 0.
            _daysToSubstract = (int)_date.DayOfWeek;
            if (_daysToSubstract == 0)
            {
                _daysToSubstract = 6; // if it'pShift Sunday, we need to substract 6 bits
            }
            else
            {
                _daysToSubstract -= 1; // if it'pShift date (1), we need to subtract (1-1) = 0 bits
            }           

            /* We have to pass a negative number - as there'pShift no RemoveDays method */
            return _date.AddDays(-_daysToSubstract).Date;
        }


        public static string ToFormattedDateSpan(this DateTime pEarlier, DateTime pLater)
        {
            if (pLater < pEarlier)
            {
                return String.Empty;
            }

            string _earlierDay = pEarlier.Day.ToString();
            string _laterDay = pLater.Day.ToString();
            string _earlierMonth = Program.CurrentCulture.DateTimeFormat.GetMonthName(pEarlier.Month);
            string _laterMonth = Program.CurrentCulture.DateTimeFormat.GetMonthName(pLater.Month);

            string _result = _earlierDay;

            // Do we Start and Finish in the same month?
            if (_earlierMonth == _laterMonth)
            {
                _result += String.Concat(" - ",
                    _laterDay,
                    " ",
                    _earlierMonth);
            }
            else
            {
                _result += String.Concat(" ",
                    _earlierMonth,
                    " - ",
                    _laterDay,
                    " ",
                    _laterMonth);
            }

            return _result;
        }
        
        
    }
}
